//调整了设置时间的逻辑，加入了每次按键就显示的功能
//1.2 修改了一些逻辑
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS1302.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h> //设置字体
//#include <LowPower.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3C
#define KEY2 8
#define KEY1 9
#define LED 10
int flag_adj = 0;                                                         //是否进入调整界面
int flag_off = 0;                                                         //关闭屏幕
int flag_mode2 = 1;                                                       //Mode2的标志，mode2按键一次点亮一次屏幕
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //OLED

const int kCePin = 5;   // 复位引脚
const int kIoPin = 6;   // 数据引脚
const int kSclkPin = 7; // 时钟引脚

DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day)
{
  switch (day)
  {
  case Time::kSunday:
    return "SUN";
  case Time::kMonday:
    return "MON";
  case Time::kTuesday:
    return "TUE";
  case Time::kWednesday:
    return "WED";
  case Time::kThursday:
    return "THU";
  case Time::kFriday:
    return "FRI";
  case Time::kSaturday:
    return "SAT";
  }
  return "(unknown day)";
}
static const uint8_t PROGMEM LOGO[] = {
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x81, 0xE1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFF, 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xFF, 0xFF, 0x9F, 0xF0, 0x1F, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xFF, 0xFF, 0xE7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x83, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

void setup()
{
  pinMode(KEY1, INPUT);
  pinMode(KEY2, INPUT);
  pinMode(LED, OUTPUT);

  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();
  display.setTextWrap(0); //不换行

  rtc.writeProtect(false);                         //去掉写保护
  rtc.halt(false);                                 //清除停止标志
  Time t(2021, 2, 6, 11, 40, 50, Time::kSaturday); //1.2写成的时间
  rtc.time(t);
}
int flag_LED = 0;
Time t = rtc.time();
const String day;
void loop()
{

  display.drawRoundRect(0, 0, 128, 64, 8, 1);
  display.display();
  delay(10);
  if (t.hr < 8) //零点到八点省电
  {
    flag_mode2 = 0;
    flag_off = 1;
  }
  else if (t.hr == 8 && (t.min < 2))
    flag_mode2 = 1; //恢复模式二
  if (!flag_adj)    //
  {
    key_adj(flag_mode2 ? 3000 : 1000);
    //开机亮灯
    if (flag_LED)
    {
      display.clearDisplay();
      display.display();
      digitalWrite(LED, HIGH);
      delay(2000);
      digitalWrite(LED, LOW);
      flag_LED = 0;
      display_logo();
    }
  }
  if (flag_adj) //进入调整界面
  {
    //调整界面
    display.clearDisplay();
    adjust_time();
    display.display();
  }
  else if (flag_off) //关闭屏幕
  {
    display.clearDisplay();
    display.display();
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  }
  else if (!flag_mode2) //mode1
  {
    display.clearDisplay();
    get_time();
    display_time(1);
    display.drawRoundRect(0, 0, 128, 64, 8, 1);
    display_data();
    display.display();
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  }
  else //mode2
  {
    display.clearDisplay();
    display.display();
    int i;
    if (scan_key2())
      for (i = 10; i > 0; i--) //修改了逻辑
      {
        digitalWrite(LED, HIGH);
        get_time();
        display_time(2);
        display.drawRoundRect(0, 0, 128, 64, 8, 1);
        display_data();
        display.display();
        display.clearDisplay();
        delay(400);
        digitalWrite(LED, LOW);
      }
    // LowPower.idle(SLEEP_15MS, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_ON, USART0_OFF, TWI_OFF);
    LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF);
  }
}

int flag_page2 = 0;
int flag_page3 = 0;
int flag_page4 = 0;
void adjust_time()
{
  //rtc.halt(false);//停止走时
  digitalWrite(LED, HIGH);
  display.drawRoundRect(0, 0, 128, 64, 8, 1);
  display.setTextColor(WHITE);
  display.setFont(&FreeSansBold9pt7b);
  int y = 20;
  int x = 13;
  static int item = 1;
  if (digitalRead(KEY1))
  {
    delay(50);
    if (digitalRead(KEY1))
    {
      item++;
      if (item > 4)
        item = 1;
    }
  }

  display_adjust_arrow(item);
  if (!flag_page2)
  {
    adjust_page1(item);
    display_adjust_page_1(y, x);
  }
  else if (!flag_page3)
  {
    adjust_page2(item);
    display_adjust_page_2(y, x);
  }
  else if (!flag_page4)
  {
    adjust_page3(item);
    display_adjust_page_3(y, x);
  }
  else
  {
    adjust_page4(item);
    display_adjust_page_4(y, x);
  }
}
void adjust_page1(int item)
{
  if (scan_key2())
    switch (item)
    {
    case 1:
      t.hr++;
      if (t.hr > 23)
        t.hr = 0;
      return;
    case 2:
      t.min++;
      if (t.min > 59)
        t.min = 0;
      return;
    case 3:
      t.sec++;
      if (t.sec > 59)
        t.sec = 0;
      return;
    case 4:
      flag_page2 = 1;
      return;
    }
}
int t_day = t.day;
void adjust_page2(int item)
{
  if (scan_key2())
    switch (item)
    {
    case 1:
      t_day++;
      if (t_day > 7)
        t_day = 1;
      adjust_t_day(t_day);
      return;
    case 2:
      t.mon++;
      if (t.mon > 12)
        t.mon = 1;
      return;
    case 3:
      t.date++;
      if (t.date > 31)
        t.date = 1;
      return;
    case 4:
      flag_page3 = 1;
      return;
    }
}
void adjust_page3(int item)
{

  if (scan_key2())
    switch (item)
    {
    case 1:
      t.yr++;
      if (t.yr > 2099)
        t.yr = 2000;
      return;
    case 2:
      display_logo();
      quit_dis();
      return; //quit仅仅退出此种模式
    case 3:
      quit_dis();
      flag_mode2 = !flag_mode2;
      return;
    case 4:
      flag_page4 = 1;
      return;
    }
}
int flag_12H = 0;
void adjust_page4(int item)
{
  if (scan_key2())
    switch (item)
    {
    case 1:
      flag_12H = !flag_12H;
      return;
      // case 2:display_logo();quit_dis();return;//quit仅仅退出此种模式
    case 3:
      apply_adj();
      return;
    case 4:
      quit_dis();
      return;
    }
}
void display_adjust_page_1(int y_adj, int x_adj) //item表示正在处理的内容
{
  display.setCursor(y_adj, x_adj);
  display.print("hour  ");
  display.setCursor(y_adj + 78, x_adj);
  display.print(t.hr);
  display.setCursor(y_adj, x_adj + 16);
  display.print("minute  ");
  display.setCursor(y_adj + 78, x_adj + 16);
  display.print(t.min);
  display.setCursor(y_adj, x_adj + 32);
  display.print("second  ");
  display.setCursor(y_adj + 78, x_adj + 32);
  display.print(t.sec);
  display.setCursor(y_adj, x_adj + 48);
  display.print("next page");

  //处理item到响应的调整阶段
  //读取现在的时间 并更新时间
  //长按进入键，将调整后时间更新到时钟模块中
}
void display_adjust_page_2(int y_adj, int x_adj)
{
  display.setCursor(y_adj, x_adj);
  display.print("day");
  display.setCursor(y_adj + 78, x_adj);
  display.print(t_day);

  display.setCursor(y_adj, x_adj + 16);
  display.print("month");
  display.setCursor(y_adj + 78, x_adj + 16);
  display.print(t.mon);
  display.setCursor(y_adj, x_adj + 32);
  display.print("data");
  display.setCursor(y_adj + 78, x_adj + 32);
  display.print(t.date);
  display.setCursor(y_adj, x_adj + 48);
  display.print("next page");
}

void display_adjust_page_3(int y_adj, int x_adj)
{
  display.setCursor(y_adj, x_adj);
  display.print("year");
  display.setCursor(y_adj + 78, x_adj);
  display.print(t.yr - 2000);

  display.setCursor(y_adj, x_adj + 16);
  display.print("logo");

  display.setCursor(y_adj, x_adj + 32);
  display.print("mode");

  display.setCursor(y_adj, x_adj + 48);
  display.print("next page");
}

void display_adjust_page_4(int y_adj, int x_adj)
{
  display.setCursor(y_adj, x_adj);
  display.print("12h or 24h");
  /*display.setCursor(y_adj+78,x_adj);
 display.print(t_day);

 display.setCursor(y_adj,x_adj+16);
 display.print("month");
 display.setCursor(y_adj+78,x_adj+16);
 display.print(t.mon);*/
  display.setCursor(y_adj, x_adj + 32);
  display.print("apply");

  display.setCursor(y_adj, x_adj + 48);
  display.print("just quit");
}

void display_logo()
{
  flag_adj = 0;
  flag_page2 = 0;
  flag_page3 = 0;
  flag_page4 = 0;
  display.clearDisplay();
  display.display();
  display.drawBitmap(0, 0, LOGO, 128, 64, WHITE);
  display.display();
  delay(1000);
  return;
}
void apply_adj()
{
  flag_adj = 0;
  flag_page2 = 0;
  flag_page3 = 0;
  flag_page4 = 0;
  rtc.time(t);
  digitalWrite(LED, LOW);
}
void quit_dis()
{
  flag_adj = 0;
  flag_page2 = 0;
  flag_page3 = 0;
  flag_page4 = 0;
  get_time(); //just_quit不改变时间
  digitalWrite(LED, LOW);
}
void adjust_t_day(int t_day)
{
  switch (t_day)
  {
  case 1:
    t.day = 2;
    return;
  case 2:
    t.day = 3;
    return;
  case 3:
    t.day = 4;
    return;
  case 4:
    t.day = 5;
    return;
  case 5:
    t.day = 6;
    return;
  case 6:
    t.day = 7;
    return;
  case 7:
    t.day = 1;
    return;
  }
}

int scan_key2()
{
  if (digitalRead(KEY2))
  {
    delay(100);
    if (digitalRead(KEY2))
      return 1;
  }
  return 0;
}
void display_adjust_arrow(int x)
{
  int x_adj;
  x_adj = 12 + 16 * (x - 1);
  display.setCursor(5, x_adj);
  display.print(">");
}

void key_adj(int time_ms)
{
  if (digitalRead(KEY1) || digitalRead(KEY2))
  {
    delay(time_ms);
    if (digitalRead(KEY1))
    {
      flag_adj = 1; //长按KEY1进入调整界面
    }
    if (digitalRead(KEY2))
    {
      flag_off = !flag_off; //长按KEY2关闭屏幕
      flag_LED = 1;
    }
  }
}

void get_time()
{
  t = rtc.time();
  day = dayAsString(t.day);
}

void display_data() //显示日期
{
  display.setTextColor(BLACK);
  display.setFont(&FreeSansBold9pt7b);

  display.fillRoundRect(4, 4, 120, 22, 7, 1);
  display.setCursor(10, 21); //行号在后
  display.print(day);
  if (t.mon >= 10 && t.date >= 10)
    display.setCursor(51, 21);
  else if (t.mon >= 10 || t.date >= 10)
    display.setCursor(57, 21);
  else
    display.setCursor(64, 21);
  display.print(t.yr - 2000);
  display.print("/");
  display.print(t.mon);
  display.print("/");
  display.print(t.date);
}

#define dis_high 56
void display_time(int mode) //不显示秒
{
  //设置颜色
  display.setTextColor(WHITE);
  display.clearDisplay();
  int hour_cursor;
  int minute_cursor;
  int second_cursor;
  if (mode == 1)
  {
    hour_cursor = 5;
    minute_cursor = 57;
    second_cursor = 97;
  }
  else if (mode == 2)
  {
    hour_cursor = 7;
    minute_cursor = 61;
    second_cursor = 101;
  }
  //hour
  display.setFont(&FreeSansBold18pt7b);
  int t_hr = t.hr;
  if (flag_12H)
    if (t_hr > 12)
      t_hr -= 12;
  if (t_hr < 10)
  {
    display.setCursor(hour_cursor, dis_high);
    display.print("0");
    display.print(t_hr);
  }
  else
  {
    display.setCursor(hour_cursor, dis_high);
    display.print(t_hr);
  }
  if (t.min < 10)
  {
    display.setCursor(minute_cursor, dis_high);
    display.print("0");
    display.print(t.min);
  }
  else
  {
    display.setCursor(minute_cursor, dis_high);
    display.print(t.min);
  }
  if (mode == 1)
  {
    display.setCursor(minute_cursor - 12, dis_high - 2);
    display.print(":");
    display.setFont(&FreeSansBold9pt7b);
    //上午或下午
    if (t.hr > 12)
    {
      display.setCursor(second_cursor, dis_high);
      display.print("pm");
    }
    else
    {
      display.setCursor(second_cursor, dis_high);
      display.print("am");
    }
  }
  if (mode == 2)
  {
    display.setFont(&FreeSansBold9pt7b);

    if (t.sec < 10)
    {
      display.setCursor(second_cursor, dis_high);
      display.print("0");
      display.print(t.sec);
    }
    else
    {
      display.setCursor(second_cursor, dis_high);
      display.print(t.sec);
    }

    if (t.sec % 2 == 0) //闪烁的点
    {
      display.setFont(&FreeSansBold18pt7b);
      display.setCursor(minute_cursor - 12, dis_high - 2);
      display.print(":");
    }
  }
}
